
layui.use(['jquery','layer','form','upload'],function () {

    var layer = layui.layer
        ,$ = layui.jquery
        ,form = layui.form
        ,upload = layui.upload;

    //获取当前时间
    var newTime;
    getTime();
    function getTime(){
        var myDate = new Date();
        var myYear = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
        var myMonth = myDate.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var myToday = myDate.getDate(); //获取当前日(1-31)
        var myDay = myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
        var myHour = myDate.getHours(); //获取当前小时数(0-23)
        var myMinute = myDate.getMinutes(); //获取当前分钟数(0-59)
        var mySecond = myDate.getSeconds(); //获取当前秒数(0-59)
        newTime = myYear+'-'+myMonth+'-'+myToday+' '+myHour+':'+myMinute+':'+mySecond;
    };

    //获取当前用户信息
    var user = JSON.parse(localStorage.getItem("user"));
    var updateBy = user.trueName;


    /*课程分类弹框*/
    classification = function classification() {
        layer.open({
            type: 2
            ,title: '选择课程分类'
            ,content: 'popup/courseClassification.html'
            ,area: ['500px',' 350px']
        })
    }

    /*封面上传*/
    upload.render({
         elem: '#imgUpload'
        ,auto: true
        ,url: '/Upload/upload'
        ,field: 'file'
        ,done:function (res) {
            var src = res.data;
            $('input[name=url]').val(src);
        }
        ,choose:function(obj){
            var files = obj.pushFile();
            obj.preview(function(index,file,result){
                $('#upload1Img').attr('src',result);
            })
        }
    });

    /*视频上传*/
    var duration;
    var uploadInst = upload.render({
         elem: '#videoUpload'
        ,url: '/upload/uploadCourseVideo'
        ,auto: true
        ,field: 'fil'
        ,accept: 'video'
        ,done:function (res) {
            var src = res.data;
            if(src.indexOf(':')){
                var path = src.split('F:\\gongcehng\\idea\\siyun-classroom/src/main/resources/static');
                $('input[name=courseUrl]').val(path[1]);

                //获取视频时长
                $(document).ready(function(){
                    $("#uploadVideo").on(
                        "timeupdate",
                        function(event){
                            duration = this.duration;
                        });
                })

            }else{
            }
        }
        ,choose:function(obj){
            var files = obj.pushFile();
            obj.preview(function(index,file,result){
                $('#uploadVideo').attr('src',result);
            })
        }
    });


    //监听上下架开关
    var courseStatus = 3;
    form.on('switch(kg)',function(obj){
        var checked = obj.elem.checked;
        if(checked){
            courseStatus = 2;
        }else{
            courseStatus = 3;
        }
    });


    /*监听提交*/
    form.on('submit()',function (data) {
        var course = data.field;
        //当前用户id
        var userId= user.userId;

        course.courseStatus = courseStatus;
        course.userId = userId;
        course.courseLabel = course.classification;
        course.coursePrimary = course.classification;
        course.createId = user.trueName;
        course.createDate = newTime;
        course.updateId = user.trueName;
        course.updateDate = newTime;


        $.ajax({
             url: '/course/addCourse'
            ,data: course
            ,dataType: 'json'
            ,success:function (data) {
                if(data.data){
                    //添加操作日志
                    $.ajax({
                        url: '/course/addoperationLog'
                        ,data: {
                            'dictSort': 1
                            ,'updateBy': updateBy
                            ,'updateTime': newTime
                            ,'remark': '执行了添加操作'
                        }
                        ,dataType: 'json'
                        ,success:function(data){

                        }
                    });
                    parent.$('#quickEntry').val('课程列表');
                    parent.quickEntry();
                    layer.msg('添加成功');
                }
            }
        });
    });
    //返回
    backOff = function backOff() {
        var arr = localStorage.getItem("numArr");
        var numArr = arr.split(",");
        numArr.pop();
        window.parent.layui.element.tabChange('tab',numArr[numArr.length - 1]);
        localStorage.setItem("numArr",numArr);

    }
    //刷新
    refresh = function refresh(){
        window.location.reload();
    }

});
